package ru.arriah.servicedesk.web.commonaction;

import java.io.IOException;
import java.util.Calendar;
import java.util.Collection;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ru.arriah.common.web.action.ActionMapping;
import ru.arriah.common.web.action.DumbAction;
import ru.arriah.servicedesk.bean.IntWrapper;
import ru.arriah.servicedesk.bean.TaskBean;
import ru.arriah.servicedesk.bean.TaskStatusBean;
import ru.arriah.servicedesk.ejb.RequestManagerLocalHome;
import ru.arriah.servicedesk.ejb.RequestManagerLocalObject;
import ru.arriah.servicedesk.help.Utils;
import ru.arriah.servicedesk.web.commonaction.exception.InternalException;

public class DisplayTasksListAction extends DumbAction {
   protected RequestManagerLocalHome requestManagerHome;

   public DisplayTasksListAction() throws NamingException {
      super();

      requestManagerHome =
         Utils.getRequestManagerHomeInterface();
   }

   public String execute(
      ActionMapping actionMapping,
      HttpServletRequest request,
      HttpServletResponse response
   ) throws IOException, ServletException {
      try {
        int executorId = Utils.getUserId(request);
        
        int page = idVal(request.getParameter("page"));        
        int itemsPerPage = Integer.parseInt(config.getProperty("ITEMS_PER_PAGE"));
        IntWrapper records = new IntWrapper(0);
        
        String order = request.getParameter("order");
        String orderBy = request.getParameter("orderBy");
        
        order = (order == null ? "" : order);
        orderBy = (orderBy == null ? "" : orderBy);
        
        RequestManagerLocalObject requestManager = 
          requestManagerHome.create();
        
        Collection<TaskBean> tasksList = 
          requestManager.selectTasksByExecutor(
             executorId, 
             orderBy, 
             order,
             page,
             itemsPerPage,
             records
          );
          
        Collection<TaskStatusBean> statusList = 
           requestManager.selectTaskStatus();     
        
        requestManager.remove();
        
        this.pageNavigationToRequest(
           page,          
           records.getValue(), 
           request.getSession().getAttribute("alias") + "?_action=listTasks&orderBy="+orderBy+"&order="+order+"&page=",
           request
        );
        
        //date attributes
        Calendar calendar = Calendar.getInstance();
        
        request.setAttribute("toStartDate", calendar.getTime());                        
        request.setAttribute("toEndDate", calendar.getTime());
        
        calendar.add(Calendar.MONTH, -1);
        request.setAttribute("fromStartDate", calendar.getTime());                        
        request.setAttribute("fromEndDate", calendar.getTime());
                
        
        request.setAttribute("action", "listTasks");
        request.setAttribute("statusList", statusList); 
        request.setAttribute("tasksList", tasksList);
        request.setAttribute("order", (order.equals("ASC") ? "DESC" : "ASC"));
        
        return actionMapping.getSuccessTarget();
      } catch (Exception e) {
         handleException(e, "execute");
         throw new InternalException("ErrorMessage.internal");
      }
   }
}